<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
    /**
     * 自定义验证规则
     */
    function validateForm() {
        form.bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                imageInfo_1: {
                    validators: {
                        notEmpty: {message: "请上传图片"},
                        file: {maxSize: 1024 * 1024, message: '请上传一张最大不超过1M的jpg/png/gif图片文件'}
                    }
                },
                sortName: {
                    validators: {
                        notEmpty: {message: "PC分类名称不可为空"},
                        stringLength: {min:2,max: 50, message: "PC分类名称长度为2至50"}
                    }
                },
                sortAppName: {
                    validators: {
                        notEmpty: {message: "APP分类名称不可为空"},
                        stringLength: {min:2,max: 50, message: "APP分类名称长度为2至50"}
                    }
                },
                sortCode: {
                    validators: {
                        stringLength: {max: 4, message: "分类排序最大长度为5"},
                        numeric: {
                            message: '只能输入数字'
                        }
                    }
                },
                categoryDescription: {
                    validators: {
                        stringLength: {min:2,max: 150, message: "分类描述长度为2至150"}
                    }
                },
                link: {
                    validators: {
                        notEmpty: {message: "链接不可为空"},
                        stringLength: {max: 200, message: "链接最大长度为200"},
                        regexp: {
                            regexp: validateUtil.webUrl,
                            message: '请输入有效的网址'
                        }
                    }
                }
            }
        });
    }

    /**
     * 添加首页中间分类左右两侧数据
     */
	<shiro:hasPermission name="back_gotoShopHomeMiddleCategoryBilateralPage_add">
    function addInfo() {
        $("#myModalLabel").empty().text("添加首页中间分类左右两侧数据");
        /**清空隐藏域的值**/
        $("#imageUrl_1").val("");
        $("#bilateralId").val("");
        $("#createTime").val("");
        $("#publishUser").val("");
        /**设置固定隐藏域的值  通过左侧的树节点点击展示右侧的子节点列表，则节点的id为列表的子节点的父id**/
        $("#categoryId").val($("#q_categoryId_bilateral").val());
        /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
        initImageForAdd(1,"sys_shop", "image_homeMiddleCategoryBilateral", 1, 500, 500);
        resetAddForm();
    }
	</shiro:hasPermission>

    /**
     * 编辑首页中间分类左右两侧数据
     */
	<shiro:hasPermission name="back_gotoShopHomeMiddleCategoryBilateralPage_update">
    function editInfo() {
        $("#myModalLabel").empty().text("修改首页中间分类左右两侧数据");
        /**获取选中行的id**/
        var selectedRow = $("#tt_bilateral").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            var id = selectedRow[0].bilateralId;
            $("#btn_edit").attr("data-target", "#addOrEditModal");/**选中了记录后才可打开对应的模态框**/
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "${basePath}/back/shopHomeMiddleCategoryBilateral/getShopHomeMiddleCategoryBilateralObj.sjson",
                data: {bilateralId: id},
                success: function (result) {
                    var data = JSON.parse(result.data);
                    /**隐藏域**/
                    $("#categoryId").val(data.categoryId);
                    $("#imageUrl_1").val(data.imageUrl);
                    $("#createTime").val(new Date(data.createTime).Format("yyyy-MM-dd hh:mm:ss"));
                    $("#publishUser").val(data.publishUser);
                    $("#bilateralId").val(data.bilateralId);
                    /**可见表单控件**/
                    $("#type_"+data.type).attr("checked","checked");
                    $("#isShow_"+data.isShow).attr("checked","checked");
                    $("#title").val(data.title);
                    $("#synopsis").val(data.synopsis);
                    $("#link").val(data.link);
                    $("#sortCode").val(data.sortCode);

                    /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
                    initImageForUpdate(1,"sys_shop", "image_homeMiddleCategoryBilateral", 1, 500, 500, "${uploadPath}", data.imageUrl);
                    reValidateForm();
                    removeValidateField('imageInfo_1');
                }
            });
        } else {
            $("#btn_edit").attr("data-target", "");/**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
    </shiro:hasPermission>

    /**
     * 重置查询表单
     */
    function resetQueryFormBilateral(){
        $("#qform_bilateral")[0].reset();
    }

    /**
     * 表单异步提交
     */
    /** 1:没有选中文件上传或没有上传文件功能 2：选中未上传 3：选中文件上传成功**/
    var fileUploadState=1;
    var isValid;
    function submitFormBilateral(){
        /** 开启验证**/
        form.data('bootstrapValidator').validate();
        isValid=form.data('bootstrapValidator').isValid();
        if(isValid){  /** 验证通过后提交表单**/
        var options = {
                url: form.attr("action"),
                type : "post",
                dataType:"json",
                success : function(data) {
                    if(data.success==true){
                        /** 先重置查询框的值**/
                        resetQueryFormBilateral();
                        /** 重新刷新列表数据**/
                        $("#tt_bilateral").bootstrapTable('refresh');
                        /** 自动关闭弹出模态框窗口**/
                        $(".close").click();
                        showMsg("保存成功！");
                    }else{
                        showMsg("保存失败！");
                    }
                }
            };
            if(fileUploadState==1||fileUploadState==3){
                form.ajaxSubmit(options);
                return true;
            }else{
                showMsg("请上传已选中的图片或重新上传图片！");
                return false;
            }
        }else{ /** 验证失败了直接返回 **/

        }
    }

</script>

<!--添加编辑模态框（Modal） -->
<div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel"></h5>
			</div>
			<div class="modal-body">
				<form id="form1" method="post" action="${basePath}/back/shopHomeMiddleCategoryBilateral/saveOrUpdateShopHomeMiddleCategoryBilateral.sjson">
					<input id="bilateralId" type="hidden" name="bilateralId" value="" >
					<input id="categoryId" type="hidden" name="categoryId" value="" noclear="true">
					<input id="createTime" type="hidden" name="createTime" value="">
					<input id="publishUser" type="hidden" name="publishUser" value="">
					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">
						<tr>
							<td class="toright_td"><span style="color:red">* </span>图片</td>
							<td class="toleft_td" colspan="3">
								<div class="container">
									<input id="imageUrl_1" type="hidden" name="imageUrl" value=""/>
									<div class="form-group" id="fileWrap1"></div>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td"><span style="color:red">* </span>类型:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<c:forEach items="${redis:getHomeKeyBook('middleBilateral')}" var="kb">
									<input type="radio"  id="type_${kb.value}" name="type" value="${kb.value}" checked/>${kb.name}&nbsp;&nbsp;
								</c:forEach>
							</td>
						</tr>
						<tr>
							<td class="toright_td"><span style="color:red">* </span>标题:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="title" name="title" placeholder="请输入标题"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td"><span style="color:red">* </span>描述:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="synopsis" name="synopsis" placeholder="请输入描述"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td"><span style="color:red">* </span>网址链接:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="link" name="link" placeholder="请输入网址链接"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>排序号:&nbsp;&nbsp;</td>
							<td class="toleft_td" >
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="sortCode" name="sortCode" placeholder="请输入排序号"/>
								</div>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>是否显示:&nbsp;&nbsp;</td>
							<td class="toleft_td">
								<label class="checkbox-inline">
									<input type="radio" name="isShow" id="isShow_1" class="radioInput" value="1" checked="checked">&nbsp;已显示
								</label>
								<label class="checkbox-inline">
									<input type="radio" name="isShow" id="isShow_0" class="radioInput" value="0">&nbsp;未显示
								</label>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary" onclick="submitFormBilateral()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
